//
//  MeViewController.m
//  智慧小区
//
//  Created by lab603 on 15/6/19.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "MeViewController.h"
#import "PPMeCell.h"
#import "PPUserViewCell.h"
#import "PPMeCellFrame.h"
#import "PPUserCellFrame.h"
#import "PPMeTextCell.h"
#import "PPMeTextCellFrame.h"
#import "PPMe.h"
#import "PPHelpController.h"
#import "AFNetWorking.h"
#import "MJExtension.h"
#import "Tool.h"
#import "LoginViewController.h"
#import "IWNavigationController.h"
#import "PPMeUserInfoController.h"
#import "EGOCache.h"
#import "UserInfo.h"
#import "UMSocial.h"
#define Cache_UserInfoKey @"UserInfoCache"
#define CacheTime 24*60*60
@interface MeViewController ()<UIActionSheetDelegate,UMSocialUIDelegate>
//其他的信息组
@property (nonatomic,strong)NSMutableArray *itemsArray;
//用户的信息组
@property (nonatomic,strong)NSMutableArray *userArray;
//只包含文字的信息组
@property (nonatomic,strong)NSMutableArray *textArray;
//持久化的用户模型
@property (nonatomic,strong)UserInfo *userInfo;
@end

@implementation MeViewController
/**
 *  用户模型的懒加载
 */
- (UserInfo *)userInfo{
    if (_userInfo==nil) {
        _userInfo = (UserInfo *)[[EGOCache globalCache] objectForKey:Cache_UserInfoKey];
    }
    return _userInfo;
}
- (NSMutableArray *)itemsArray{
    if (_itemsArray==nil) {
        _itemsArray = [NSMutableArray array];
        PPMe *me1 = [[PPMe alloc] init];
        me1.icon = @"MoreShare.png";
        me1.typeString = @"分享给朋友";
        PPMeCellFrame *meF1 = [[PPMeCellFrame alloc] init];
        meF1.me = me1;
        [_itemsArray addObject:meF1];
        
        PPMe *me2 = [[PPMe alloc] init];
        me2.icon = @"MorePush.png";
        me2.typeString = @"应用评分";
        PPMeCellFrame *meF2 = [[PPMeCellFrame alloc] init];
        meF2.me = me2;
        [_itemsArray addObject:meF2];
        
        PPMe *me3 = [[PPMe alloc] init];
        me3.icon = @"MoreHelp.png";
        me3.typeString = @"帮助中心";
        PPMeCellFrame *meF3 = [[PPMeCellFrame alloc] init];
        meF3.me = me3;
        [_itemsArray addObject:meF3];
        
        
    }
    return _itemsArray;
}

- (NSMutableArray *)userArray{
    if (_userArray==nil) {
        _userArray = [NSMutableArray array];
        //取出保存在本地的用户模型
        PPMe *user = [[PPMe alloc] init];
        user.icon = self.userInfo.icon;
        user.typeString = self.userInfo.name;
        user.editString = @"编辑个人信息";
        PPUserCellFrame *userF = [[PPUserCellFrame alloc] init];
        userF.me = user;
        [_userArray addObject:userF];
    }
    return _userArray;
}

- (NSMutableArray *)textArray{
    if (_textArray==nil) {
        _textArray = [NSMutableArray array];
        PPMe *text = [[PPMe alloc] init];
        text.typeString = @"退出登录";
        PPMeTextCellFrame *textF = [[PPMeTextCellFrame alloc] init];
        textF.me = text;
        [_textArray addObject:textF];
    }
    return _textArray;
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title = @"个人中心";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = IWColor(239, 239, 239);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger number = 0;
    if (section==0) {
        number = self.userArray.count;
    }
    else if(section==1)
    {
        number = self.itemsArray.count;
    }else if(section==2){
        number = self.textArray.count;
    }
    return number;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id realCell;
    if (indexPath.section==0) {
        PPUserViewCell *cell = [PPUserViewCell cellWithTabelView:tableView];
        PPUserCellFrame *userCellF = self.userArray[indexPath.row];
        cell.usercellFrame = userCellF;
        realCell = cell;
    }else if(indexPath.section==1){
        PPMeCell *cell = [PPMeCell cellWithTableView:tableView];
        PPMeCellFrame *cellF = self.itemsArray[indexPath.row];
        cell.cellF = cellF;
        realCell = cell;
    }else if (indexPath.section==2){
        PPMeTextCell *cell = [PPMeTextCell cellWithTableView:tableView];
        PPMeTextCellFrame *cellF = self.textArray[indexPath.row];
        cell.textCellF = cellF;
        realCell = cell;
    }
    return realCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight = 0;
    if (indexPath.section==0) {
        PPUserCellFrame *usercellF = self.userArray[indexPath.row];
        cellHeight = usercellF.cellheight;
    }else if(indexPath.section==1){
        PPMeCellFrame *cellF = self.itemsArray[indexPath.row];
        cellHeight = cellF.cellheight;
    }else if (indexPath.section==2){
        PPMeTextCellFrame *textF = self.textArray[indexPath.row];
        cellHeight = textF.cellheight;
    }
    return cellHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
/**
 *  更新用户头像
 */
- (void)refreshUserIcon:(UserInfo *)userInfo{
    self.userInfo=nil;
    PPUserCellFrame *userF = [self.userArray lastObject];
    userF.me.icon = userInfo.icon;
    [self.userArray removeAllObjects];
    [self.userArray addObject:userF];
    [self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        PPMeUserInfoController *userInfoVC = [[PPMeUserInfoController alloc] init];
        //当
        [userInfoVC setUserIconRefreshBlock:^(UserInfo *userInfo) {
            //更新用户头像
            [self refreshUserIcon:userInfo];
        }];
        [self.navigationController pushViewController:userInfoVC animated:YES];
    }else if (indexPath.section==1) {
        switch ((indexPath.row+1)) {
            case 1:{
                NSString *shareURL = [NSString stringWithFormat:@"http://itunes.apple.com/us/app/id%d", 1026645836];
                //设置标题
                [UMSocialData defaultData].extConfig.title = @"Hi小区";
                //设置微信的分享链接
                [UMSocialData defaultData].extConfig.wechatSessionData.url = shareURL;
                //设置微信朋友圈的分享链接
                [UMSocialData defaultData].extConfig.wechatTimelineData.url = shareURL;
                //分享QQ的分享链接
                [UMSocialData defaultData].extConfig.qqData.url = shareURL;
                //分享QQ空间的链接
                [UMSocialData defaultData].extConfig.qzoneData.url = shareURL;
                //分享到其他平台
                [UMSocialSnsService presentSnsIconSheetView:self
                                                     appKey:UMENG_APPID
                                                  shareText:@"欢迎使用Hi小区应用"
                                                 shareImage:[UIImage imageNamed:@"IconImage.png"]
                                            shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,nil]
                                                   delegate:self];

            }
                break;
            case 2:{
                //跳转应用评价
                NSString *str = [NSString stringWithFormat:
                                 @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",
                                 1026645836];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            }
                break;
            case 3:{
                PPHelpController *helpVC = [[PPHelpController alloc] init];
                [self.navigationController pushViewController:helpVC animated:YES];
            }
                break;
            default:
                break;
        }
    }else if(indexPath.section==2){
        //弹出提示栏，选择是否退出
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                 delegate:self
                                                        cancelButtonTitle:@"取消"
                                                   destructiveButtonTitle:@"退出登录"
                                                        otherButtonTitles:nil, nil];
        [actionSheet showInView:self.view];
        //取消选中状态
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    }
    
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *titile = [actionSheet buttonTitleAtIndex:buttonIndex];
    if ([titile isEqualToString:@"退出登录"]) {
        LoginViewController *loginVc = [[LoginViewController alloc] init];
        IWNavigationController *nVC = [[IWNavigationController alloc] initWithRootViewController:loginVc];
        [self.view.window setRootViewController:nVC];
    }
}
/**
 *  实现分享后回调方法
 */
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        [Tool HUDShowMessage:@"分享成功" addedToView:[UIApplication sharedApplication].keyWindow];
    }
}

@end